/**
*********************************************************************************
 *(C) @@.        [hx] All Rights Reserved.
 *FileName:      uart_data_wrap.c
 *Author:        fly
 *Telephtone:    
 *Version:       1.0.0
 *Data:          2021.01.04
 *Description:   
 *Others:        
 *History:       
**********************************************************************************
*/

#include "serial_data.h"
#include "crc.h"

U16 uart_base_wrap(U8 *wrap_buffer, server_cmd_t *server_cmd){
    U16 pos = 0;
    //��䱾��Mac��ַ
    //extern global_config_t global_config;//ȫ������
    //memcpy(wrap_buffer + pos, global_config.ap_mac, sizeof(global_config.ap_mac));
    //pos += sizeof(global_config.ap_mac);
    //���Index
    wrap_buffer[pos++] = ((server_cmd->index >> 24) & 0xff);
    wrap_buffer[pos++] = ((server_cmd->index >> 16) & 0xff);
    wrap_buffer[pos++] = ((server_cmd->index >> 8) & 0xff);
    wrap_buffer[pos++] = ((server_cmd->index >> 0) & 0xff);
    //���type
    wrap_buffer[pos++] = server_cmd->type;
    //������ݳ���
    wrap_buffer[pos++] = ((server_cmd->length >> 8) & 0xff);
    wrap_buffer[pos++] = ((server_cmd->length >> 0) & 0xff);
    return pos;
}

PTL_STATUS uart_unpack(const ComStream_t * inStream,UnPack_t *pResult){
	//1Ѱ�Ұ�ͷ
	bool bHasHead = false;
	for (U32 i = 0; i < inStream->nLenOfBuf; i++)
	{
		if (inStream->pBuffer[i] == UART_DATA_HEAD)
		{
			if (i > 0 && inStream->pBuffer[i - 1] == UART_DATA_TRANSLATE)
				continue;
			bHasHead = true;
			pResult->indexOfHead = i;//��ð�ͷ
			break;
		}
	}
	//���û���ҵ���ͷ������һ������
	if (!bHasHead)
		return PTL_HEAD_ERROR;

	//2.Ѱ�Ұ�β(�Ӱ�ͷ��ʼ)
	U16 nTailPos = 0;
	for (U32 i = pResult->indexOfHead + 1; i < inStream->nLenOfBuf; i++)
	{
		if (inStream->pBuffer[i] == UART_DATA_TAIL)
		{
			if ( inStream->pBuffer[i - 1] == UART_DATA_TRANSLATE)
				continue;
			nTailPos = i;
			break;
		}
		/*if (inStream->pBuffer[i] == UART_DATA_HEAD)
		{
			nTailPos = i - 1;
			break;
		}*/
	}

	//��������(���ƻ���ȴ���������)
	if (nTailPos < 1)
		return PTL_LENGTH_ERROR;

	//3.��һ�������з�ת��(������ͷ����β)
	//uint16_t nLenofTransData = nTailPos - pResult->indexOfHead + 1;
	U16 nLenofActData = 0;
	U8 pTempBuf[MAX_PACK_LEN]; //= new u8[nLenofTransData];

	for (U16 i = pResult->indexOfHead + 1; i < nTailPos; i++)
	{
		U8 curChar = inStream->pBuffer[i];
		if (curChar == UART_DATA_TRANSLATE)
			pTempBuf[nLenofActData++] = inStream->pBuffer[++i] - 1;
		else
			pTempBuf[nLenofActData++] = inStream->pBuffer[i];
	}

	
	//4.��֤������:2byte
	U16 nLength = (pTempBuf[0] << 8) + pTempBuf[1];
	//nLenofActData(length + option + cmd +data+ crc)  nLength(option + cmd + data)
	if (nLenofActData != nLength + 3)
	{
		pResult->indexOfHead = nTailPos + 1;
		return PTL_LENGTH_ERROR;
	}

	
	//��ȡβ������
	pResult->indexOfTail = nTailPos;

	//6.��֤У���루length + option + cmd +data��
	U8 cCRC8 = cal_crc8_table(pTempBuf, nLength + 2);

	
	
	if (cCRC8 != pTempBuf[nLenofActData - 1])
	{
		return PTL_CHECKSUM_ERROR;
	}

	//���Э��ѡ��
	pResult->option = pTempBuf[2];
	//6.��ȡ����ID
	pResult->cmdID = pTempBuf[3];

	//7.��ȡ�����򳤶�
	pResult->lenOfData = nLength - 2;

	//8.��ȡ������
	memcpy(pResult->pData, &pTempBuf[4], pResult->lenOfData);

	return PTL_NO_ERROR;


}		

PTL_STATUS uart_pack(const Pack_t*  pPack, ComStream_t* outStream){
	if (pPack == NULL || outStream == NULL)
		return PTL_DATA_ERROR;

    U8 pPackAct[MAX_PACK_LEN]; //= new u8[nLenOfData + 6];
	//if (!pPackAct)
	//	return PTL_DATA_ERROR;

	//1.����ͷ
	U16 pos = 0;
	pPackAct[pos++] = UART_DATA_HEAD;

	//2. ��䳤��(option + cmd + nLenOfData) 
	U16 nLength = pPack->nLenOfData + 2;
	pPackAct[pos++] = (nLength >> 8) & 0xff;
	pPackAct[pos++] = (nLength >> 0) & 0xff;
	//3.Э��ѡ��
	pPackAct[pos++] = pPack->option;
	//3.�������ID
	pPackAct[pos++] = pPack->cmdID;

	//4.���������
	memcpy(&pPackAct[pos], pPack->pData, pPack->nLenOfData);
	pos += pPack->nLenOfData;
	//5.���У���ֽ�(У�� length + option + cmd + data)
	pPackAct[pos++] = cal_crc8_table(pPackAct + 1, 2+1+1+pPack->nLenOfData);
	
	//6.����β
	pPackAct[pos++] = UART_DATA_TAIL;

	//7.�����ݰ�����ת�壨������ͷ����β length + option + cmd + crc8��
	U16 nLenOfTransData = 0;
	outStream->pBuffer[0] = pPackAct[0];
	for (U16 i = 1; i < pPack->nLenOfData + 6; i++)
	{
		U8 curChar = pPackAct[i];
		if (curChar == UART_DATA_HEAD || curChar == UART_DATA_TAIL || curChar == UART_DATA_TRANSLATE)
		{
			outStream->pBuffer[nLenOfTransData + 1] = UART_DATA_TRANSLATE;
			outStream->pBuffer[nLenOfTransData + 2] = curChar + 1;
			nLenOfTransData++;
		}
		else
			outStream->pBuffer[nLenOfTransData + 1] = curChar;

		nLenOfTransData++;
	}
	outStream->pBuffer[nLenOfTransData + 1] = pPackAct[6 + pPack->nLenOfData];

	//���ذ�����
	outStream->nLenOfBuf = nLenOfTransData + 2;

	return PTL_NO_ERROR;
}



